package com.rentalHome.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.rentalHome.domain.Rental;
import com.rentalHome.domain.Room;
import com.rentalHome.domain.User;

/**
*
* @author LeeXyan
* @date 2017年2月4日 上午9:57:11
*
**/

public interface RentalRepository extends PagingAndSortingRepository<Rental, Long> {

	List<Rental> findAllByUser(User user);
	
	@Query("from Rental r where r.user = :user and r.isValid = true order by r.nextPayDate")
	List<Rental> findCurrentRentalByUserASCNextPayDate(@Param("user") User user);
	
	@Query("from Rental r where r.user = :user and r.isValid = false order by r.room.name")
	List<Rental> findHistoryRentalByUser(@Param("user") User user);

	@Query("from Rental r where r.room = :room and r.isValid = false order by r.room.name")
	List<Rental> findHistoryRentalByRoom(@Param("room") Room room);

	List<Rental> findByUserId(Long id);

	@Query("from Rental r where r.user = :user and r.isValid = true order by r.room.name")
	List<Rental> findCurrentRentalByUser(@Param("user") User user);
	
	@Query("from Rental r where r.user = :user and r.room = :room and r.isValid = false order by r.room.name")
	List<Rental> findAllRentalByUserAndRoom(@Param("user") User user, @Param("room") Room room);

	@Query("from Rental r where r.room = :room and r.customerName = :customerName order by r.room.name")
	Rental findByRoomAndCustomerName(@Param("room") Room room,@Param("customerName")String customerName);

	@Query("from Rental r where r.room = :room and r.customerName = :customerName and r.user = :user order by r.room.name")
	Rental findByRoomAndCustomerNameAndUser(@Param("room") Room room,@Param("customerName")String customerName,@Param("user") User user);

	@Query("from Rental r where r.customerName =:customerName")
	Rental findByCustomerName(@Param("customerName")String customerName);
	
}
